<?php
namespace Crm\Table;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;

/**
 * 客户管理
 *        
 */
class UserTable extends AbstractTable
{

    protected $table = 'hy_user';

    protected $id = 'user_id';

    public function find($query = array())
    {
        $select = new Select(array(
            'u' => $this->getTable()
        ));
        
        $select->join(
            array('org' => 'hy_organization'),
            'u.org_id = org.org_id',
            array('org_name', 'org_code') ,
            $select::JOIN_LEFT
        );
        
        if (! empty($query['user_name'])) {
            $spec = function (Where $where) use($query) {
                $where->like('u.user_name', "%" . $query['user_name'] . "%");
            };
            
            $select->where($spec);
        }
        if(!empty($query['org_id'])) {
            $select->where(array('u.org_id' => $query['org_id']));
        }
        if(!empty($query['user_uid'])) {
            $select->where(array('u.user_uid' => $query['user_uid']));
        }
        if(!empty($_SESSION['account']['org_id'])) {
            $select->where(array('u.org_id' => $_SESSION['account']['org_id']));
        }
        
        $select->order('u.user_id DESC');
        return $this->fetchPaginate($select, $query);
    }
    public function findAll($query = array())
    {
        $select = new Select(array(
            'u' => $this->getTable()
        ));
        
        $select->join(
            array('org' => 'hy_organization'),
            'u.org_id = org.org_id',
            array('org_name') ,
            $select::JOIN_LEFT
        );
        
        if (! empty($query['user_name'])) {
            $spec = function (Where $where) use($query) {
                $where->like('u.user_name', "%" . $query['user_name'] . "%");
            };
            
            $select->where($spec);
        }
        if(!empty($query['org_id'])) {
            $select->where(array('u.org_id' => $query['org_id']));
        }
        if(!empty($query['user_uid'])) {
            $select->where(array('u.user_uid' => $query['user_uid']));
        }
        if(!empty($_SESSION['account']['org_id'])) {
            $select->where(array('u.org_id' => $_SESSION['account']['org_id']));
        }
        
        $select->order('u.user_id DESC');
        $rowset = $this->selectWith($select);
        $list = $rowset->toArray();
    
        return $list;
    }

    public function findOne($id)
    {
        $rowset = $this->select(array(
            'user_id' => $id
        ));
        $row = $rowset->current();
        return $row;
    }

    public function getUser()
    {
        $resultSet = $this->getAdapter()->query('SELECT * FROM `hy_user` WHERE `user_id` = ?', array(
            1
        ));
        
        return $resultSet->current();
    }
    
    public function updateBalance($id,$balance){
        if(empty($id)){
            throw new \Exception("id field is invalid!");
        }
        $item = array(
            'balance'=>$balance,
        );
        return $this->update($item,array('user_id'=>$id));
    }
}